{% extends 'user/base.html' %}

{% block title %}
    <title>结算</title>
{% endblock %}

{% block content %}
<section class="slice pt-lg-3 pb-2 bg-section-secondary">
    <div class="container mt-4 mb-5">
        <!-- 新增：Flash 消息显示区域 -->
        <div class="flash-messages">
            {% with messages = get_flashed_messages(with_categories=true) %}
                {% if messages %}
                    {% for category, message in messages %}
                        <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                            {{ message }}
                            <button type="button" class="close" data-bs-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                    {% endfor %}
                {% endif %}
            {% endwith %}
        </div>

        <h5 class="font-weight-bold border-bottom pb-2">订单结算</h5>

        <!-- 新增地址的表单 -->
        <form method="POST" action="{{ url_for('cart.checkout') }}">
            <div class="mt-3">
                <button type="button" class="btn btn-secondary btn-sm" data-bs-toggle="collapse" data-bs-target="#newAddressForm">
                    新增收货地址
                </button>
                <div id="newAddressForm" class="collapse mt-3">
                    <div class="form-group">
                        <label for="recipient_name">收件人姓名</label>
                        <input type="text" class="form-control" name="recipient_name" id="recipient_name" required>
                    </div>
                    <div class="form-group">
                        <label for="phone">联系电话</label>
                        <input type="text" class="form-control" name="phone" id="phone" required>
                    </div>
                    <div class="form-group">
                        <label for="address_line">详细地址</label>
                        <input type="text" class="form-control" name="address_line" id="address_line" required>
                    </div>
                    <button type="submit" class="btn btn-primary" name="action" value="add_address">保存地址</button>
                </div>
            </div>
        </form>

        <!-- 确认支付的表单 -->
        <form method="POST" action="{{ url_for('cart.checkout') }}">
            <div class="form-group mt-3">
                <label for="address_id">选择收货地址</label>
                <select class="form-control" name="address_id" required>
                    {% if addresses %}
                        {% for address in addresses %}
                            <option value="{{ address.id }}">
                                {{ address.recipient_name }} - {{ address.phone }} - {{ address.address_line }}
                            </option>
                        {% endfor %}
                    {% else %}
                        <option value="" disabled selected>没有地址，请新增地址</option>
                    {% endif %}
                </select>
            </div>

            <div class="payment-info mt-4">
                <h6>支付方式</h6>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="payment_method" id="alipay" value="alipay" checked>
                    <label class="form-check-label" for="alipay">
                        支付宝支付
                    </label>
                </div>
            </div>

            <button type="submit" class="btn btn-primary mt-4">确认支付</button>
        </form>
    </div>
</section>
{% endblock %}

{% block extendjs %}
{% endblock %}